use "MayerSmithPartisanshipCC_DATA.dta", clear

//partisanship
***Expressive
polychoric eff_part1-eff_part8
mat eff=r(R)
local n=e(N)
factormat eff, n(`n') ipf fa(1)
predict drop_eff
egen expressive = std(drop_eff)
la var expressive "Expressive Partisanship"


****Negative
polychoric neg_part1-neg_part5
mat neg=r(R)
local n=e(N)
factormat neg, n(`n') ipf fa(1)
predict drop_neg
egen neg_part = std(drop_neg)
la var neg_part "Negative Partisanship"

****Create partisan scales by affilitiation (just for histogram)
foreach p in expressive neg_part {
clonevar `p'_dem = `p'
replace `p'_dem=. if party_id2==1
clonevar `p'_gop = `p'
replace `p'_gop=. if party_id2==2
}

//Trust variables
*Government
polychoric trust_pres trust_cong trust_slc
mat trust_ins=r(R)
local n=e(N)
factormat trust_ins, n(`n') ipf
predict drop_trust_ins
egen trust_ins = std(drop_trust_ins)
la var trust_ins "Trust in Government"


*Social trust
polychoric trust_neigh trust_fam trust_pplnat trust_ppl1
mat trust_soc=r(R)
local n=e(N)
factormat trust_soc, n(`n') ipf
predict drop_trust_soc
egen trust_soc = std(drop_trust_soc)
la var trust_soc "Trust in Society"

*Science
polychoric sci_trust_cc sci_trust_vax sci_trust_nuc sci_trust_evo
mat trust_sci=r(R)
local n=e(N)
factormat trust_sci, n(`n') ipf
predict drop_trust_sci
egen trust_sci = std(drop_trust_sci)
la var trust_sci "Trust in Science"

la var trust_ins "Institutional Trust" 
la var trust_soc "Social Trust"
la var trust_sci "Scientific Trust"

//Reliability of Indicators
****Cronbachs
alpha neg_part1-neg_part5

alpha eff_part1-eff_part8

alpha trust_pres trust_cong trust_slc

alpha trust_neigh trust_fam trust_pplnat trust_ppl1

alpha sci_trust_cc sci_trust_vax sci_trust_nuc sci_trust_evo

****Split half
*Negative Partisanship
egen neg1=rowtotal(neg_part1 neg_part3 neg_part5), missing
egen neg2=rowtotal(neg_part2 neg_part4), missing

corr neg1 neg2
local neg_sbpf = 2*r(rho)/(1+r(rho))
di `neg_sbpf'

*Expressive Partisanship
egen exp1=rowtotal(eff_part1 eff_part3 eff_part5 eff_part7), missing
egen exp2=rowtotal(eff_part2 eff_part4 eff_part6 eff_part8), missing

corr exp1 exp2
local exp_sbpf = 2*r(rho)/(1+r(rho))
di `exp_sbpf'

*Trust in society
egen soc_trust1=rowtotal(trust_neigh trust_pplnat), missing
egen soc_trust2=rowtotal(trust_fam trust_ppl1), missing

corr soc_trust1 soc_trust2
local soc_trust_sbpf = 2*r(rho)/(1+r(rho))
di `soc_trust_sbpf'

*Trust in Governent
egen ins_trust1=rowtotal(trust_pres trust_slc), missing
egen ins_trust2=rowtotal(trust_cong), missing

corr ins_trust1 ins_trust2
local ins_trust_sbpf = 2*r(rho)/(1+r(rho))
di `ins_trust_sbpf'

*Trust in science
egen sci_trust1=rowtotal(sci_trust_cc sci_trust_nuc), missing
egen sci_trust2=rowtotal(sci_trust_vax  sci_trust_evo), missing

corr sci_trust1 sci_trust2
local sci_trust_sbpf = 2*r(rho)/(1+r(rho))
di `sci_trust_sbpf'

//Drop unneeded
drop drop_*  ///
	eff_part1-eff_part8 neg_part1-neg_part5 ///
	trust_pres trust_cong trust_slc ///
	trust_neigh trust_fam trust_pplnat trust_ppl1 ///
	sci_trust* ///
	neg1 neg2 exp1 exp2 ///
	soc_trust1 soc_trust2 ///
	ins_trust1 ins_trust2
